<?php
declare (strict_types=1);

namespace app\common\queue\bokecc;

use app\common\exception\LogicException;
use app\common\library\Queue;
use app\common\model\Attach;
use eduline\upload\stocks\bokecc\File;
use Exception;
use think\exception\FileException;
use think\facade\{Db, Request};

/**
 * bokecc同步视频分类
 */
class Update
{

    /**
     * run
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/08
     */
    public function run()
    {
        $attachId = Request::post('attachId');
        $cloudId  = Request::post('cloudId');
        try {
            $times = cache($cloudId) ?: 0;
            if ($times >= 10) return;
            // 附件未上传云端，队列处理
            $attach = app(Attach::class)->findOrEmpty($attachId);
            if ($attach->isEmpty()) return;
            if ($attach->status == 2) {
                Queue::push('BokeccUpdate', ['attachId' => $attachId, 'cloudId' => $cloudId], 10);
                return;
            }
            // 附件上传云端，修改云端视频分类
            $bokecc = new File();
            $re     = $bokecc->updateVideo($attachId, $cloudId);
            //
            if (isset($re['error'])) {
                cache($cloudId, $times + 1);
                // (new Db)->name('test')->save(['msg' => 'bokecc update error' . $re['error']]);
                Queue::push('BokeccUpdate', ['attachId' => $attachId, 'cloudId' => $cloudId], 10);
            }
        } catch (FileException|Exception $e) {
            throw new LogicException($e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】');
        }
    }

}
